#include "OfflineCommandPage.h"

#include "LoopControlInfoManager.h"
#include "LoopManager.h"
#include "CommandExecutor.h"


void OfflineCommandPage::beforeDisplay() {
    mpageSize = LoopControlInfoManagerSingleton::instance().getLoopControlInfos(std::chrono::system_clock::now().time_since_epoch()).size();
    if (0 == mpageSize){
        mpageSize = 1;
    }
    mmultiPageNo = 0;
}



void OfflineCommandPage::paint() {
    if (focus) {
        super::paint();
    } else {
        std::string content[4];

        LoopControlInfoPtr pLoopControlInfo = LoopControlInfoManagerSingleton::instance().locateLoopControlInfo(std::chrono::system_clock::now().time_since_epoch(), mmultiPageNo*mfactor + mbase);
        resetStream();
        strStream << std::setw(13) << std::left << mtitle;
        strStream << std::setw(3)  << std::right << pageNum;
        content[0] = strStream.str();

        std::string_view offlineCommand;
        if (!pLoopControlInfo) {
            content[1] = "00000000 000000";

            LoopManagerSingleton::instance().updatePortCount();
            offlineCommand = *LoopManagerSingleton::instance().mpTimedLoopControlInfo;
        } else {
            DateTimeUtility::tmFormatToString(DateTimeUtility::toTm(DateTimeUtility::toTime_t(pLoopControlInfo->beginTime)), "%Y%m%d %H%M%S", content[1]);

            offlineCommand = *pLoopControlInfo;
        }


        resetStream();
        strStream << std::setw(9) << std::left;
        if (offlineCommand.length() > Screen::SCREEN_WIDTH) {
            strStream << offlineCommand.substr(Screen::SCREEN_WIDTH, 9);

            offlineCommand.remove_suffix(offlineCommand.length() - Screen::SCREEN_WIDTH);
        } else {
            strStream << ' ';
        }
        strStream << std::setw(7) << std::right << "delete";
        content[3] = strStream.str();

        content[2] = offlineCommand;

        screen.show(&content[0], &content[1], &content[2], &content[3]);
    }

}

    void OfflineCommandPage::saveValue() {
        std::string result;
        CommandExecutorSingleton::instance().onDeleteLocalSwitch(result);
    }

